package com.manger.utils;

import com.manger.model.Project;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

public class CopyFiles {

    public static void copyBaseFiles(String targetDir, Project project) throws IOException {
        File file = new File(SystemConstant.getConfigDir() + "/" + project.getDebtName());
        if(!file.exists()){
            return;
        }
        File[] files = file.listFiles();
        for(File fileDir : files){
            File[] tempFiles = fileDir.listFiles();
            for(File tempFile : tempFiles){
                String targetPath = targetDir + File.separator + fileDir.getName() + File.separator + tempFile.getName();
                File tempFileConfig = new File(targetPath);
                if(tempFileConfig.exists()){
                   continue;
                }
                if(!tempFileConfig.getParentFile().exists()){
                    tempFileConfig.getParentFile().mkdirs();
                }
                Files.copy(Paths.get(tempFile.getAbsolutePath()),Paths.get(targetPath));
            }
        }
    }

    /*public static void copyFileToFile(String resourcePath,String targetPath) throws Exception {
        if(resourcePath.toLowerCase().endsWith("doc") || resourcePath.toLowerCase().endsWith("docx")
                || resourcePath.toLowerCase().endsWith("xls") || resourcePath.toLowerCase().endsWith("xlsx")
                || resourcePath.toLowerCase().endsWith("ppt") || resourcePath.toLowerCase().endsWith("pptx")){
            //转换
            targetPath = targetPath.substring(0,targetPath.lastIndexOf(".")) + ".pdf";
            File2PdfUtils.file2Pdf(resourcePath,targetPath);
            return;
        }
        Files.copy(Paths.get(resourcePath),Paths.get(targetPath));
    }*/

    public static void copyFileToFile(String resourcePath,String targetPath) throws Exception {
        Files.copy(Paths.get(resourcePath),Paths.get(targetPath));
    }

    public static void copyFileToDir(String resourcePath,String targetDir) throws Exception{
        copyFileToDir(resourcePath,targetDir,"");
    }

    public static void copyDirToDir(String resourceDir,String targetDir) throws Exception{
        File resourceDirFile = new File(resourceDir);
        if(!resourceDirFile.exists()){
            return;
        }
        File[] files = resourceDirFile.listFiles();
        for(File file : files){
            copyFileToDir(file.getCanonicalPath(),targetDir);
        }
    }

    public static void copyOtherMaterialFileToDir(String extractDir,Map<String,String> idCardMap) throws Exception{
        String otherFilePath = extractDir + "/" + SystemConstant.CASE_FILE_DIR_NAME;
        File otherFile = new File(otherFilePath);
        if(!otherFile.exists()){
            return;
        }
        System.out.println("整理压缩包中材料数据!");
        File[] dirs = otherFile.listFiles();
        for (File dir : dirs) {
            String dirName = dir.getName();
            LoanFileEnum loanFileEnum = LoanFileEnum.getLoanFileEnum(dirName);
            if (loanFileEnum == null) {
                continue;
            }
            File[] files = dir.listFiles();
            for (File file : files) {
                String targetDirPath = "";
                for (String key : idCardMap.keySet()) {
                    if (file.getName().contains(key)) {
                        targetDirPath = idCardMap.get(key);
                        break;
                    }
                }
                if (StringUtils.isEmpty(targetDirPath)) {
                    continue;
                }
                CopyFiles.copyFileToDir(file.getAbsolutePath(), targetDirPath + "/" + dirName);
            }
        }
    }

    /*public static String getFileNameMark(String fileName, List<String> tagetList) {
        for (String mark : tagetList) {
            if (fileName.contains(mark)) {
                return mark;
            }
        }
        return null;
    }*/
    public static boolean checkHasCopyFileToDirByOnlyOneFileNameMarks(String fileName,List<String> onlyOneFileNameMarks,String targetDir) {
        boolean hasExist = false;
        String tempMark = "";
        for(String mark: onlyOneFileNameMarks){
            if(fileName.contains(mark)){
                hasExist = true;
                tempMark = mark;
                break;
            }
        }
        if(!hasExist){
            return true;
        }
        File dir = new File(targetDir + "/" + LoanFileEnum.EVIDENCE_MATERIALS.getName());
        if(!dir.exists()){
            return true;
        }
        File[] files = dir.listFiles();
        for(File file : files){
            if(file.getName().contains(tempMark)){
                return false;
            }
        }
        return true;
    }

    public static boolean hasFileNameMark(String fileName, List<String> tagetList) {
        for (String mark : tagetList) {
            if (fileName.contains(mark)) {
                return true;
            }
        }
        return false;
    }

    public static void copyFileToDir(String resourcePath,String targetDir,String mark) throws Exception {
       /* if(resourcePath.toLowerCase().endsWith("doc") || resourcePath.toLowerCase().endsWith("docx")
                || resourcePath.toLowerCase().endsWith("xls") || resourcePath.toLowerCase().endsWith("xlsx")
                || resourcePath.toLowerCase().endsWith("ppt") || resourcePath.toLowerCase().endsWith("pptx")){
            //转换
            String pdfTargetPath = resourcePath.substring(0, resourcePath.lastIndexOf(".")) + ".pdf";
            if(StringUtils.isNotEmpty(mark)){
                 pdfTargetPath = resourcePath.substring(0, resourcePath.lastIndexOf(".")) + "_" + mark + ".pdf";
            }
            File2PdfUtils.file2Pdf(resourcePath,pdfTargetPath);
            return;
        }*/
        String fileName = new File(resourcePath).getName();
        fileName = fileName.replaceAll(" ","");
        String targetFilePath = targetDir + File.separator  + fileName;
        if(StringUtils.isNotEmpty(mark)){
            targetFilePath = targetDir + File.separator + mark + "_"  + fileName;
        }
        File file = new File(targetFilePath);
        if(file.exists()){
            return;
        }
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        Files.copy(Paths.get(resourcePath),Paths.get(targetFilePath));
    }

    public static void createDirs(String loanUserDir){
        LoanFileEnum[] loanFileEnums = LoanFileEnum.values();
        for(LoanFileEnum loanFileEnum : loanFileEnums){
           String tempFilePath = loanUserDir + File.separator + loanFileEnum.getName();
           File file = new File(tempFilePath);
           file.mkdirs();
        }
    }

    public static LoanFileEnum getLoanFileEnum(String fileName, Map<String,LoanFileEnum> fileMapping) {
        for(String key: fileMapping.keySet()){
            if(fileName.toLowerCase().contains(key.toLowerCase())){
                return fileMapping.get(key);
            }
        }
        return LoanFileEnum.EVIDENCE_MATERIALS;
    }
}
